#-*- coding: utf-8 -*-

import copy

# a與b都參照到mutable的物件
a = b = [ "dog", "cat", "pig" ]

print "before b.append()"
print "a is: " + str( a )
print "b is: " + str( b )

print "\n=============================\n"

b.append( "ribbit" )	# 直接修改原物件
print "after b.append()"
print "a is: " + str( a )
print "b is: " + str( b )

print "\n=============================\n"

i = [1, 2]
j = i
print "before"
print "i is: " + str( i )
print "j is: " + str( j )

j = j + [3, 4]	# 對j所參考的List做串接，再重新指派給j變數

print "after"
print "i is: " + str( i )
print "j is: " + str( j )

print "\n=============================\n"

l = [100, 200]
m = l
print "before"
print "l is: " + str( l )
print "m is: " + str( m )

l += [300, 400]  # 相當於l.extend( [300, 400] ) 
# l.extend( [300, 400] )

'''
當對List或Dictionary這種mutable的物件使用複合指派運算子時，不會產生新的物件，
會直接對原物件做修改，這是Python效能最佳化的一種方式!
'''

print "after"
print "l is: " + str( l )
print "m is: " + str( m )

print "\n=============================\n"

mutableList = [1, 2, 3, 4, 5, 6]
mutableList.append( [10, 20, 30] )	# 正確的串加，因為List物件是mutable，所以方法會直接作用在原List上
print "mutableList is: " + str( mutableList )

print "\n=============================\n"

mutableList = [1, 2, 3, 4, 5, 6]
mutableList = mutableList.append( [11, 22, 33] ) # 錯誤的寫法，append()會回傳None，重新指派None給mutableList
print "mutableList is: " + str( mutableList )

print "\n=============================\n"

mutableList = [1, 2, 3, 4, 5, 6]
newList = mutableList.append( [78, 23, 903] )
print "mutableList is: " + str( mutableList )  # [ 1, 2, 3, 4, 5, 6, [78, 23, 903] ]
print "newList is: " + str( newList )	# None

print "\n=============================\n"

oldList = [10, 20, 30]
newList = copy.deepcopy( oldList )	# 指派oldList的複本給newList變數
oldList.append( 40 )
print "oldList is: " + str( oldList )
print "newList is: " + str( newList )